package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.support.v7.widget.SearchView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.SearchAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Search;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.SearchModel;

/**
 * Created by Lix on 2018/10/23.
 */

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {

    private SearchView searchView;
    private RecyclerView recyclerView;
    private ArrayList<Search> mSearch;
    private SearchAdapter searchAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        initEvent();
        SearchModel searchModel = new SearchModel();
        searchModel.getSearch(searchListener);

    }

    List_TListener<Search> searchListener = new List_TListener<Search>() {
        @Override
        public void onResponse(List<Search> list) {
            mSearch = new ArrayList<>();
            mSearch = (ArrayList<Search>) list;

        }

        @Override
        public void onFail(String msg) {

        }
    };


    private void initView(){
        searchView = (SearchView)findViewById(R.id.searchEdit);
      //  searchView.setIconifiedByDefault(true);
        searchView.setFocusable(false);
        searchView.clearFocus();

        recyclerView = (RecyclerView)findViewById(R.id.rv_search);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

    }
    private void initEvent(){
        // 设置搜索文本监听
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    searchAdapter = new SearchAdapter(getApplicationContext(), filter(mSearch,newText));
                    recyclerView.setAdapter(searchAdapter);
                    //Toast.makeText(SearchActivity.this,Integer.toString(filter(mSearch,newText).size()), Toast.LENGTH_SHORT).show();
                }else{
                    List<Search> list = new ArrayList<>();
                    searchAdapter = new SearchAdapter(getApplicationContext(),list);
                    recyclerView.setAdapter(searchAdapter);
                }
                return false;
            }
        });

    }
    private ArrayList<Search> filter(ArrayList<Search> strings,String text) {
        ArrayList<Search> filterString=new ArrayList<>();
        Log.i("mSearch的size:",Integer.toString(strings.size()));
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).getFlag()==1) {
                if (strings.get(i).getClub_name().contains(text)) {
                    filterString.add(strings.get(i));
                }
            }
            if (strings.get(i).getFlag()==2) {
                if (strings.get(i).getTrain_name().contains(text)) {
                    filterString.add(strings.get(i));
                }
            }
            if (strings.get(i).getFlag()==3) {
                if (strings.get(i).getMaster_name().contains(text)) {
                    filterString.add(strings.get(i));
                }
            }

        }
        return filterString;
    }


    @Override
    public void onClick(View v) {

    }
}
